import store from "@/store"
import { getFieldListByComponentList } from "./analysisData"
import { getComponentListByKeys } from "./utils"
import { getPropsList, resetAttr } from "../custom-components/attr"
/**
 * 获取下拉框的数据,即下拉框筛选字段的options
 * @param { Component[] } analysisComponentList
 */
export async function getDateList(analysisComponentList){
  getDateFilterFieldListOptions(analysisComponentList)
  .then(tableList => {
    store.commit("lowCode/setDateFilterFieldList", tableList)
  })
}


/**
 * 获取业务组件和字段的列表options
 * @param { Component[] } analysisComponentList
 * @returns { Promise<GroupOption[]> }
 */
export async function getDateFilterFieldListOptions(analysisComponentList){
  const tableList = await getFieldListByComponentList(analysisComponentList, true, ["DATETIME"])
  return tableList
}

/**
 * 日期选择器change回调,更新数据
 * @param { string } fieldName
 * @param { string[] } value
 */
export function setDateSelectFields(fieldName, value){
  const {
    canvasComponentList,
    selectedComponent // 当前绑定的过滤组件
  } = store.state.lowCode

  // 拿到当前选中的字段的数据,也就是携带的parentId(绑定的业务组件id)
  // const analysisComponentKey = option.data.attrs['data-parent-id']
  const analysisComponentKeys = value.map(item => item.split('/')[0])
  const paramsList = []
  // 当前绑定的业务组件
  const analysisComponentList = getComponentListByKeys(canvasComponentList, analysisComponentKeys)
  // 遍历所有业务组件
  analysisComponentList.forEach((analysisComponent,dataIndex) => {
    const index = analysisComponent.props.filterKeys.indexOf(selectedComponent.key)
    // 如果当前下拉框,选择了当前组件的某个字段
    if (value.find(item => item.split('/')[0] === analysisComponent.key)){
      // 如果当前业务组件已经绑定了该过滤组件,就不处理,如果未绑定,则添加
      if (index === -1){
        analysisComponent.props.filterKeys.push(selectedComponent.key)
      }
    }
    // 如果未选择
    else {
      // 如果当前业务组件已经绑定了该过滤组件,则删除该业务组件与下拉框的绑定,不过未绑定,则不处理
      if (index > -1){
        analysisComponent.props.filterKeys.splice(index, 1)
      }
    }
  })
  // console.log(paramsList)
  // const columnName = value[0].split('/')[1]
  resetAttr(store, fieldName, value)
  // if(value.length > 0){
  //   // 单维度
  //   Promise.all(paramsList.map(params => getTableOption(3,columnName,params)))
  //   .then(data => {
  //     const options = []
  //     data.forEach(item => {
  //       item.forEach(obj => {
  //         if(!options.find(option => option.value === obj.value)){
  //           options.push(obj)
  //         }
  //       })

  //     })
  //     selectedComponent.props.options = options
  //     // 设置初始值
  //     resetAttr(store, fieldName, value)
  //   })
  // }
  // else {
  //   selectedComponent.props.options = []
  //   // 清空初始值
  //   resetAttr(store, fieldName, value)
  // }
}
